package com.bree.erp.jsf.view;

import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

import org.primefaces.model.LazyDataModel;

import com.bree.erp.entity.BalanceId;
import com.bree.erp.entity.Project;
import com.bree.erp.entity.ReturnsBalance;
import com.bree.erp.entity.Warehouse;
import com.bree.erp.jsf.datatable.LazyBreeDataModel;
import com.bree.erp.jsf.httpsess.UserSessionData;
import com.bree.erp.service.ProjectService;
import com.bree.erp.service.ReturnsBalanceService;
import com.bree.erp.service.ReturnsServiceFacade;
import com.bree.erp.service.WarehouseService;

@ManagedBean(name = "dtReturnsBalanceLazyView")
@ViewScoped
public class ReturnsBalanceLazyView {
	
	private LazyDataModel<ReturnsBalance> lazyModel;

	@EJB
	private ReturnsBalanceService balanceService;
	
	@EJB
	private ProjectService projectService;
	
	@EJB
	private WarehouseService warehouseService;
	
	@EJB
	private ReturnsServiceFacade returnsFacade;
	
	@ManagedProperty(value="#{dtPartUnitLazyView}")
	private PartUnitLazyView partUnitLazyView;

	@ManagedProperty(value="#{userSession}")
	private UserSessionData userSession;
	

	private Date entryDate;
	private Long projectId;
	private String warehouseId;
	private double quantity;
	private String location;
	private String remarks;

    @PostConstruct
    public void init() {
    	lazyModel = new LazyBreeDataModel<ReturnsBalance, ReturnsBalanceService, BalanceId>(balanceService, new BalanceId());
    }
    
    public void add() {
    	
		Long partUnitId = partUnitLazyView.getSelectedItem().getId();
    	String userId = userSession.getLoggedInUser().getEmail();
		returnsFacade.addReturns(entryDate, projectId, warehouseId, partUnitId, location, quantity, remarks, userId );
    }
	
	public List<Project> getProjects() {
		return projectService.list();
	}
	
	public List<Warehouse> getWarehouses() {
		return warehouseService.list();
	}

	/**
	 * @return the lazyModel
	 */
	public LazyDataModel<ReturnsBalance> getLazyModel() {
		return lazyModel;
	}

	/**
	 * @param lazyModel the lazyModel to set
	 */
	public void setLazyModel(LazyDataModel<ReturnsBalance> lazyModel) {
		this.lazyModel = lazyModel;
	}

	/**
	 * @return the projectId
	 */
	public Long getProjectId() {
		return projectId;
	}

	/**
	 * @param projectId the projectId to set
	 */
	public void setProjectId(Long projectId) {
		this.projectId = projectId;
	}

	/**
	 * @return the warehouseId
	 */
	public String getWarehouseId() {
		return warehouseId;
	}

	/**
	 * @param warehouseId the warehouseId to set
	 */
	public void setWarehouseId(String warehouseId) {
		this.warehouseId = warehouseId;
	}

	/**
	 * @return the location
	 */
	public String getLocation() {
		return location;
	}

	/**
	 * @param location the location to set
	 */
	public void setLocation(String location) {
		this.location = location;
	}

	/**
	 * @return the remarks
	 */
	public String getRemarks() {
		return remarks;
	}

	/**
	 * @param remarks the remarks to set
	 */
	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}

	/**
	 * @return the quantity
	 */
	public double getQuantity() {
		return quantity;
	}

	/**
	 * @param quantity the quantity to set
	 */
	public void setQuantity(double quantity) {
		this.quantity = quantity;
	}

	/**
	 * @return the entryDate
	 */
	public Date getEntryDate() {
		return entryDate;
	}

	/**
	 * @param entryDate the entryDate to set
	 */
	public void setEntryDate(Date entryDate) {
		this.entryDate = entryDate;
	}

	/**
	 * @return the partUnitLazyView
	 */
	public PartUnitLazyView getPartUnitLazyView() {
		return partUnitLazyView;
	}

	/**
	 * @param partUnitLazyView the partUnitLazyView to set
	 */
	public void setPartUnitLazyView(PartUnitLazyView partUnitLazyView) {
		this.partUnitLazyView = partUnitLazyView;
	}

	/**
	 * @return the userSession
	 */
	public UserSessionData getUserSession() {
		return userSession;
	}

	/**
	 * @param userSession the userSession to set
	 */
	public void setUserSession(UserSessionData userSession) {
		this.userSession = userSession;
	}
}
